import {
  Controller,
  Get,
  Post,
  Put,
  Delete,
  Param,
  Body,
  UsePipes,
  ValidationPipe,
} from '@nestjs/common';
import { DictionaryService } from '../services/dictionary.service';
import { Dictionary } from '../models/dictionary.entity';

@Controller('dictionaries')
export class DictionaryController {
  constructor(private readonly dictionaryService: DictionaryService) {}

  @Get()
  async getDictionaries(): Promise<Dictionary[]> {
    return this.dictionaryService.findAll();
  }

  @Get(':id')
  async getDictionary(@Param('id') id: string): Promise<Dictionary | null> {
    return this.dictionaryService.findOne(Number(id));
  }

  @Post()
  @UsePipes(new ValidationPipe())
  async createDictionary(
    @Body() dictData: Partial<Dictionary>,
  ): Promise<Dictionary> {
    return this.dictionaryService.create(dictData);
  }

  @Put(':id')
  @UsePipes(new ValidationPipe())
  async updateDictionary(
    @Param('id') id: string,
    @Body() dictData: Partial<Dictionary>,
  ): Promise<Dictionary | null> {
    return this.dictionaryService.update(Number(id), dictData);
  }

  @Delete(':id')
  async deleteDictionary(@Param('id') id: string): Promise<Dictionary | null> {
    return this.dictionaryService.remove(Number(id));
  }
}
